package com.cheng.xbjy.controller;

import com.cheng.xbjy.entity.Meeting;
import com.cheng.xbjy.entity.vo.PageResult;
import com.cheng.xbjy.entity.vo.Result;
import com.cheng.xbjy.service.MeetingService;
import com.cheng.xbjy.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/meeting")
public class MeetingController {

    @Autowired
    private MeetingService meetingService;

    /**
     * 查询会议
     *
     * @param title
     * @param status
     * @param page
     * @return
     */
    @GetMapping("/findPage/{page}")
    public Result findPage(String title, Long status, @PathVariable("page") Integer page) {
        Page<Meeting> pageData = meetingService.findPage(title, status, page);
        return new Result(true, "查询成功"
                , new PageResult<Meeting>(pageData.getTotalPages(), pageData.getContent()));
    }

    /**
     * 查看会议详情
     *
     * @param meetingId
     * @return
     */
    @GetMapping("/detail/{meetingId}")
    public Result findDetail(@PathVariable("meetingId") Long meetingId) {
        Map<String, Object> map = meetingService.findDetail(meetingId, LoginUserUtil.getUserId());
        return new Result(true, "查询成功", map);
    }

    /**
     * 加入会议/取消会议
     */
    @PostMapping("/joinMeeting/{meetingId}")
    public Result joinMeeting(@PathVariable("meetingId") Long meetingId) {
        return meetingService.joinMeeting(meetingId, LoginUserUtil.getUserId());
    }

    /**
     * 添加会议
     * @param meeting
     * @return
     */
    @PostMapping("/addMeeting")
    public Result addMeeting(@RequestBody Meeting meeting) {
        meetingService.save(meeting);
        return new Result(true, "添加成功");
    }
}
